package com.lsh.dao;

import com.lsh.domain.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

/**
 * 用户dao类（User实体类）
 */
@Repository
public interface UserDao {
    // 注册用户
    @Insert("insert into users (username, password, mobile, email)" +
            " values (#{username}, #{password}, #{mobile}, #{email}) ")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void saveUser(User user);

    // 登录用户
    @Select("SELECT * FROM users" +
            " WHERE" +
            " (username = #{username} OR email = #{email} OR mobile = #{mobile})" +
            " AND password = #{password}")
    String getByUser(User user);

    //获取用户username
    @Select("select username from users where id = #{userId} ")
    String getUsername(int userId);

    //根据用户账号名，获取用户ID
    @Select("SELECT id FROM users where username = #{username}")
    int getUserId(String username);

    // 根据用户id查询页面展示的信息
    @Select("select id, username, mobile, email from users where id = #{userId}")
    User getByUserId(int userId);

    // 根据用户ID以及旧密码修改密码
    @Update("update users set password=#{newPwd} where id=#{uid} and password=#{oldPwd}")
    int updatePassword(@Param("uid") String userId, @Param("newPwd") String newPwd, @Param("oldPwd") String oldPwd);
}
